//获取当前时间戳 reutrn YYYY-MM-DD
    function getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        //var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();

        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
        return currentdate;
    };
//传入一个  YYYYY-MM-DD  前推和后退数以天数
    function nextDayFnc(startTime,y,z){
        var startTime = startTime;
        var startTime2 = startTime.split("-");
        var year = parseInt(startTime2[0]);
        var month = parseInt(startTime2[1]);
        var day = parseInt(startTime2[2]);
        var myDate=new Date();
        var setYesDay =  parseInt(myDate.setFullYear(year,month-1,day));
        if(z==1){
            var yesterday = parseInt(setYesDay-86400000*y);     //加减天数改变这个毫秒数
        }else if(z==2){
            var yesterday = parseInt(setYesDay+86400000*y);      //z==1时为-  z==2时为+
        }
        var ds = new Date(yesterday);
        var oldYear=ds.getYear()+1900;
        var oldMonth = ds.getMonth()+1;
        if (oldMonth<10){
            oldMonth = "0"+oldMonth;
        }else{
            oldMonth = oldMonth;
        }
        var oldDay = ds.getDate();
        if(oldDay<10){
            oldDay = "0"+oldDay;
        }else{
            oldDay = oldDay;
        }
        var yesDay = oldYear+"-"+oldMonth+"-"+oldDay;
        return yesDay;
    };
//传入 YYYYY-MM-DD 转成 yyyy年mm月dd日
    function parseDate(date){
        var newDate = date.split("-");
        return newDate[0]+"年"+newDate[1]+"月"+newDate[2]+"日";
    }
    var date = new Date();
    var lastWeek = new Date(date.getTime() - 6 * 24 * 3600 * 1000);
    var lastTwo = new Date(date.getTime() - 13 * 24 * 3600 * 1000);
    var tody = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    lastWeek = lastWeek.getFullYear() + "-" + (lastWeek.getMonth() + 1) + "-" + lastWeek.getDate();
    lastTwo = lastTwo.getFullYear() + "-" + (lastTwo.getMonth() + 1) + "-" + lastTwo.getDate();
    var yesterday = nextDayFnc(getNowFormatDate(), 1, 1);
    // 借用nextDayFnc方法将XXXX-X-X转化成XXXX-0X-0X格式
    tody = nextDayFnc(tody,0,1);
    lastWeek = nextDayFnc(lastWeek,0,1);
    lastTwo = nextDayFnc(lastTwo,0,1);
    $(".tody_btn").html("今日："+parseDate(tody));
	$(".tody_day").html("今日："+parseDate(tody));
	$(".yesterday_day").html("昨日："+parseDate(yesterday));
	$(".lastWeek_day").html("最近7日："+parseDate(lastWeek)+" — "+parseDate(tody));
	$(".lastTwo_day").html("最近14日："+parseDate(lastTwo)+" — "+parseDate(tody));
	
	
//	时间
	var beginDate,endDate;

//点击事件
function selectDate(d,obj){
	if(d == 1){
		beginDate=tody;
		endDate=tody;
		$(".tody_btn").html($(obj).find("a").html());
	}else if(d == 2){
		beginDate=yesterday;
		endDate=tody;
		$(".tody_btn").html($(obj).find("a").html());
	}else if(d == 3){
		beginDate=lastWeek;
		endDate=tody;
		$(".tody_btn").html($(obj).find("a").html());
	}else if(d == 4){
		beginDate=lastTwo;
		endDate=tody;
		$(".tody_btn").html($(obj).find("a").html());
	}else if(d == 5){
		var test1=$("#test1").val();
		var test2=$("#test2").val();
		if(!test1){
			layer.msg("请先选择时间");
			return false;
		}
		if(!test2){
			layer.msg("请先选择时间");
			return false;
		}
		beginDate=test1;
		endDate=test2;
		$(".tody_btn").html("时间为："+parseDate(test1)+" — "+parseDate(test2));
		$(".tody_btn").parent().removeClass("open");
	}
	p=1;
	tiaozhuan(p);
}

//代理商报表接口
var p=1;
function tiaozhuan(p){
	var pageSize=$('.ui-select-pagesize').val();
	var keyWord=$(".keyWord").val();
	var d={};
	d.pageSize=pageSize;
	d.keyWord=keyWord;
	d.page=p;
	d.beginDate=beginDate;
	d.endDate=endDate;
	$.ajax({
		url: url + "/sys/report/queryPage",
		type: 'POST',
		//		参数需转为字符串
		data: JSON.stringify(d),
		//		json跨域
		dataType: 'json',
		contentType: "application/json",
		beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		useDefaultXhrHeader: false,
		success: function(result) {
			if(result.code == '0') {
				var data=result.data.rows;
				var html="";
				for(var i in data){
					html += '<tr class="ng-scope"><td  class="ng-binding">'+data[i].adverId+'</td>';
					html += '<td  class="ng-binding">'+data[i].adverName+'</td><td  class="ng-binding">'+data[i].adverCost+'</td>';
					html += '<td  class="ng-binding">'+data[i].dspCost+'</td><td  class="ng-binding">'+data[i].profit+'</td>';
					html += '<td class="ng-binding">'+data[i].rateProfit+'</td><td  class="ng-binding">'+data[i].showTimes+'</td>';
					html += '<td  class="ng-binding">'+data[i].clickTimes+'</td><td class="ng-binding">'+data[i].rateClick+'</td>';
					html += '<td  class="ng-binding">'+data[i].unitCostCpm+'</td><td  class="ng-binding">'+data[i].unitCostCpc+'</td></tr>';
				}
				$(".list_dsp").html(html);
				var r=result.data.total;
					paging(p,pageSize,r);
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	}); 
}
tiaozhuan(1);
//下载文件
function download(){
	var param2 = "/sys/report/queryPage";
	var method2 = "post";
	var keyWord=$(".keyWord").val();
	var datas2 = {page: p, pageSize: 1000, keyWord: keyWord,beginDate:beginDate,endDate:endDate};
	getAgentMsg2(param2,method2,datas2);
	exportCsv({
        title: ['广告主ID','广告公司名称','广告公司花费(元)','DSP成本(元)','利润(元)','利润率(%)','展示量','点击量','点击率(%)','CPM单价(元)','CPC单价(元)'],
        titleForKey: ['adverId','adverName','adverCost','dspCost','profit','rateProfit','showTimes','clickTimes','rateClick','unitCostCpm','unitCostCpc'],
        data: dataResult2.data.rows,
    },"代理商报表.csv");
}

//搜索按钮
$(".keyWord_btn").on("click",function(){
	p=1;
	tiaozhuan(p);
})
